﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class RecycleResetSelectHeroPanel : RecycleSelectPanel 
    {
        public RecycleResetSelectPage page;
        public const int selectMax = 1;

        protected string selectCache = "";

        public override void SortItem(List<GameObject> objs)
        {
            objs.Sort(delegate (GameObject x, GameObject y) {
                return -x.GetComponent<RecycleSelectHeroItem>().CompareTo(y.GetComponent<RecycleSelectHeroItem>());
            });
        }

        public override void OnConfirmButtonClick()
        {
            StaticData.recycleResetSelect.Clear();
            StaticData.recycleResetSelect.Add(selectCache);
            page.Close();
        }

        public override void ClearSelectCache()
        {
            selectCache = "";
        }

        public override void InitSelectCache()
        {
            if (StaticData.recycleResetSelect.Count > 0)
            {
                selectCache = StaticData.recycleResetSelect[0];
            }
            else
            {
                selectCache = "";
            }
        }

        public override bool IsSelectMax()
        {
            return !string.IsNullOrEmpty(selectCache);
        }

        public override void AddSelectCache(string guid)
        {
            selectCache = guid;
        }

        public override void RemoveSelectCache(string guid)
        {
            if (selectCache == guid)
            {
                selectCache = "";
            }
            else
            {
                Debug.LogError("Remove Not Select GUID:" + guid);
            }
        }

        public override bool IsItemSelected(object item)
        {
            UIHero uiHero = item as UIHero;
            if (selectCache == uiHero.guid)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override List<object> GetCanSelectItems()
        {
            List<object> res = new List<object>();
            foreach (var item in StaticData.playerData.InstPlayerData.InstHeros)
            {
                if (item.Value.heroData.heroClass == (int)HeroClass.Exp_Eater)
                {
                    continue;
                }

                if (item.Value.IsZeroHero())
                {
                    continue;
                }

                if (StaticData.playerData.InstPlayerData.IsUIHeroInFormation(item.Value))
                {
                    continue;
                }
                if (StaticData.playerData.InstPlayerData.IsHeroInHelpFomation(item.Value.guid))
                {
                    continue;
                }
                res.Add(item.Value);
            }
            return res;
        }

        public override void ShowSelectMaxTip()
        {
            BoxManager.CreatePopupTextBox(TableTextBlo.GetText("recycle_select_reset_hero_warning"));
        }

        public override GameObject CreateSelectItem(object obj)
        {
            UIHero hero = obj as UIHero;
            GameObject select = null;
            foreach (var item in cachedObjs)
            {
                if (item.GetComponent<RecycleSelectItemBase>().GetItemGUID() == hero.guid)
                {
                    select = item;
                    break;
                }
            }
            if (select != null)
            {
                cachedObjs.Remove(select);
                return select;
            }
            else
            {
                return GameObject.Instantiate(itemPrefab) as GameObject;
            }
        }

    }
}